package com.hyyt.shopjsp.finance.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.finance.pojo.ShopMallCoin;
import com.hyyt.shopjsp.finance.service.IShopMallCoinService;
import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.export.ExportExcel;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/4/07 0024下午 15:18.
 */
@Controller
@RequestMapping("/back/statisticAnalysisExcel")
public class StatisticAnalysisExcelController extends BaseController {
	private Logger log = Logger.getLogger(StatisticAnalysisExcelController.class);
	
	@Resource
	private IShopMallCoinService shopMallCoinService;//金币service

	/**导出excel文件的列名称**/
	private List<String> typeNameList = new ArrayList<String>();
	/**
	 * 导出会员金币Excel
	 * @param response
	 * @param request
	 * @throws IOException 
	 */
	@RequestMapping("exportStatisticOrders")
	public void exportStatisticOrders(HttpServletResponse response, HttpServletRequest request) throws IOException{
		Map<String, Object> param=new HashMap<>();
		String loginName = request.getParameter("loginName");
		String ordersNo = request.getParameter("ordersNo");
		String registerDate = request.getParameter("registerDate");
		String registerDate2 = request.getParameter("registerDate2");
		String balanceType=request.getParameter("balanceType");
		if(Utils.objectIsNotEmpty(loginName)){
			param.put("loginName", loginName.trim());
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			param.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(registerDate)){
			param.put("registerDate", registerDate);
		}
		if(Utils.objectIsNotEmpty(registerDate2)){
			param.put("registerDate2", registerDate2);
		}
		if(Utils.objectIsNotEmpty(balanceType)){
			param.put("type", balanceType);
		}
		param.put("sortColumns", "tradeTime desc");
		List<ShopMallCoin> countList=shopMallCoinService.queryMallCoinList(param);
		SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		List<Map<String,Object>> mallCoin = new ArrayList<>();
		if(countList!=null&&countList.size()>0){
			 for (ShopMallCoin shopMallCoin : countList) {
				 Map<String,Object> map = new HashMap<>();
				 map.put("serialNumber", shopMallCoin.getSerialNumber());
				 map.put("loginName", shopMallCoin.getLoginName());
				 map.put("ordersNo",shopMallCoin.getOrdersNo());
				 map.put("transactionNumber",shopMallCoin.getTransactionNumber());
				 switch (shopMallCoin.getType()){
				 	case 1:
				 		map.put("type","收入");
				 		break;
				 	case 2:
				 		map.put("type","支出");
				 		break;
				 	case 3:
				 		map.put("type","退回");
				 		break;
				 	case 4:
				 		map.put("type","交易作废");
				 		break;
				 }
				 map.put("tradeTime",formatter.format(shopMallCoin.getTradeTime()));
				 mallCoin.add(map);
			}
		}
		String fileName = "我的金币明细表";
        String columnNames[] = {"流水号","订单号","收支数量","类型","时间"};
        String keys[] = {"serialNumber","ordersNo","transactionNumber","type","tradeTime"};
        try {
            ExportExcel.download(fileName,mallCoin,columnNames,keys,request,response);
        } catch (IOException e) {
            logger.error("导出Excel失败！");
        }
	}

}